package com.dong.rabbitmq.ackwork;

import com.dong.rabbitmq.common.Constant;
import com.dong.rabbitmq.utils.RabbitMqUtil;
import com.dong.rabbitmq.utils.SleepUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * @author dongjian
 * 消费者01
 * <p>
 * 手动消息应答，放回队列，重新消费
 */
public class WorkAckConsumer01 {

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtil.getChannel();
        System.out.println("C1等待消费消息处理消息时间较短");
        // 接收消息处理逻辑
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            // 模拟业务处理时间 1s
            SleepUtil.sleep(1);
            System.out.println("消费者01接收消息" + new String(message.getBody(), "UTF-8"));
            /**
             * 手动应答
             * 参数说明：
             * 1.消息标识 tag
             * 2.是否批量应答 false不批量应答
             */
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);

        };
        // prefetch=1:不公平分发设置
        // prefetch=n(n>1) n:预取值数
        channel.basicQos(2);
        channel.basicConsume(Constant.TASK_QUEUE_NAME, false, deliverCallback, consumerTag -> {
            System.out.println(consumerTag + "消费者取消消费消息");
        });
    }
}
